package org.example.rzfx.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.rzfx.entity.OpLog;
import org.example.rzfx.mapper.OpLogMapper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;

/**
 * 操作日志服务
 */
@Service
public class OpLogService extends ServiceImpl<OpLogMapper, OpLog> {
    
    /**
     * 保存操作日志（异步）
     */
    @Async
    public void saveOpLog(Long userId, String username, String opType, String opDesc, String ip) {
        OpLog opLog = new OpLog();
        opLog.setUserId(userId);
        opLog.setUsername(username);
        opLog.setOpType(opType);
        opLog.setOpDesc(opDesc);
        opLog.setIp(ip);
        opLog.setOpTime(LocalDateTime.now());
        
        this.save(opLog);
    }
    
    /**
     * 保存操作日志（同步）
     */
    public void saveOpLogSync(Long userId, String username, String opType, String opDesc, String ip) {
        OpLog opLog = new OpLog();
        opLog.setUserId(userId);
        opLog.setUsername(username);
        opLog.setOpType(opType);
        opLog.setOpDesc(opDesc);
        opLog.setIp(ip);
        opLog.setOpTime(LocalDateTime.now());
        
        this.save(opLog);
    }
}
